//
//  HWGameGiftBagCell.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/30.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import YLProgressBar

class HWGameGiftBagCell: UITableViewCell {
    
    var bgView:UIView!
    var gameImageView:UIImageView!
    var titleLabel:UILabel!
    var contentLabel:UILabel!
    var progressBar:YLProgressBar!
    var surplusLabel:UILabel!
    var copyButton:UIButton!
    var model:HWGameGiftBagModel? {
        didSet {
            gameImageView.sd_setImage(with: URL(string: model?.bgimg ?? ""), completed: nil)
            titleLabel.text = model?.gname
            contentLabel.text = model?.content
            surplusLabel.text = "剩余 \(model?.bfb ?? "")"
            progressBar.progress = CGFloat(((model?.cate ?? 0) - (model?.shengyu ?? 0))/(model?.cate ?? 0))
        }
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWGameGiftBagCell) {
        let reUse:String = "HWGameGiftBagCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameGiftBagCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameGiftBagCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 12*widthScale
        contentView.addSubview(bgView)
        
        gameImageView = UIImageView()
        gameImageView.backgroundColor = .clear
        gameImageView.layer.cornerRadius = 12*widthScale
        gameImageView.layer.masksToBounds = true
        bgView.addSubview(gameImageView)
        
        titleLabel = HWLabel.createBoldLabel(title: "名字", color: "#444444", font: 14*widthScale)
        bgView.addSubview(titleLabel)
        
        contentLabel = HWLabel.createBoldLabel(title: "礼包介绍", color: "#888888", font: 12*widthScale)
        contentLabel.numberOfLines = 1
        bgView.addSubview(contentLabel)
        
        progressBar = YLProgressBar()
        progressBar.indicatorTextDisplayMode = .none
        progressBar.progressTintColors = [UIColor.colorWithHexString(hexString: "#FFB60A"),UIColor.colorWithHexString(hexString: "#F66A05")]
        progressBar.trackTintColor = .colorWithHexString(hexString: "#EEEEEE")
        progressBar.cornerRadius = 0
        progressBar.hideStripes = true
        progressBar.progress = 0
        progressBar.hideGloss = true
        progressBar.progressStretch = true
        progressBar.progressBarInset = 0
        bgView.addSubview(progressBar)
        
        surplusLabel = HWLabel.createLabel(title: "", color: "#888888", font: 10*widthScale)
        bgView.addSubview(surplusLabel)
        
        copyButton = UIButton(type: .custom)
        copyButton.setTitle("复制推广", for: .normal)
        copyButton.backgroundColor = .colorWithHexString(hexString: "#5879FF")
        copyButton.layer.cornerRadius = 12*widthScale
        copyButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        copyButton.addTarget(self, action: #selector(copyButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(copyButton)
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(10*widthScale)
            make.right.equalTo(-20*widthScale)
            make.bottom.equalToSuperview()
        }
        
        gameImageView.snp.makeConstraints { make in
            make.left.top.equalTo(10*widthScale)
            make.bottom.equalTo(-13*widthScale)
            make.width.height.equalTo(52*widthScale)
        }

        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(gameImageView.snp.right).offset(8*widthScale)
            make.top.equalTo(gameImageView)
        }

        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(2*widthScale)
            make.right.equalTo(copyButton.snp.left).offset(-20*widthScale)
        }

        progressBar.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.bottom.equalTo(gameImageView)
            make.width.equalTo(100*widthScale)
            make.height.equalTo(10*widthScale)
        }

        surplusLabel.snp.makeConstraints { make in
            make.left.equalTo(progressBar.snp.right).offset(6*widthScale)
            make.centerY.equalTo(progressBar)
        }

        copyButton.snp.makeConstraints { make in
            make.top.equalTo(28*widthScale)
            make.right.equalTo(-10*widthScale)
            make.width.equalTo(68*widthScale)
            make.height.equalTo(24*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension HWGameGiftBagCell {
    @objc func copyButtonClick(_:UIButton) -> Void {
        let pboard = UIPasteboard.general
        pboard.string = model?.url
        currentWindow?.showHUD(title: "复制成功")
    }
}
